package com.example.stlink.fragments;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.stlink.R;
import com.example.stlink.activitys.fragmentChild.activityTeaHome.CheckCourseSign;
import com.example.stlink.activitys.fragmentChild.activityTeaHome.ChooseCourse;
import com.example.stlink.activitys.fragmentChild.activityTeaHome.CreateCourse;
import com.example.stlink.activitys.fragmentChild.activityTeaHome.CreateSign;
import com.example.stlink.model.constants.ModelFieldConstants;

public class TeaHomeFragment extends Fragment{

    private Button btCodeCheck;
    private Button btWordCheck;
    private Button btCreateClass;
    private Button btManageClass;

    private static ViewPager2 vp2;

    private Bundle bundle;

    private static ActivityResultLauncher<Intent> intentActivityResultLauncher;

    private static AppCompatActivity mAppCompatActivity;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tea_home, container, false);
        initView(view);
        return view;
    }

    public static TeaHomeFragment newInstance(String str, AppCompatActivity appCompatActivity, ViewPager2 viewPager2){
        mAppCompatActivity = appCompatActivity;
        TeaHomeFragment frag = new TeaHomeFragment();
        vp2 = viewPager2;
        intentActivityResultLauncher = appCompatActivity.registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            Intent data = result.getData();
            int resultCode = result.getResultCode();

            System.out.println("registerForActivityResult...........................");
            if (resultCode == Activity.RESULT_OK) {
                //将活动销毁返回的数据刷新到当前活动，同时刷新当前布局
                refreshData();
            }else if (resultCode == 100){
                assert data != null;
                mAppCompatActivity.getIntent().putExtras(data.getExtras());
                Intent intent = new Intent(mAppCompatActivity, CheckCourseSign.class);
                intent.putExtras(mAppCompatActivity.getIntent().getExtras());
                mAppCompatActivity.startActivity(intent);
            }
        });
        return frag;
    }

    /**
     * 初始化界面
     */
    private void initView(View view){
        btCodeCheck = view.findViewById(R.id.bt_code_check);
        btWordCheck = view.findViewById(R.id.bt_word_check);
        btCreateClass = view.findViewById(R.id.bt_create_class);
        btManageClass = view.findViewById(R.id.bt_manage_class);

        setClickListener();

    }

    private static void refreshData(){

    }

    /**
     * 为当前布局上的按钮添加点击监听事件
     */
    private void setClickListener(){
        //查看签到
        btCodeCheck.setOnClickListener(v -> {
            bundle = mAppCompatActivity.getIntent().getExtras();
            String nowSignCourseName = bundle.getString(ModelFieldConstants.NOW_SIGN_COURSE_NAME);
            if(nowSignCourseName != null){
                System.out.println("nowSignCourseName = " + nowSignCourseName + "......................");
                Intent intent = new Intent(mAppCompatActivity, CheckCourseSign.class);
                intent.putExtras(mAppCompatActivity.getIntent().getExtras());
                startActivity(intent);
            }else{
                Intent intent = new Intent(mAppCompatActivity, ChooseCourse.class);
                bundle.putInt(ModelFieldConstants.COURSE_CHOOSE_SELECT, 1);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        //创建签到
        btWordCheck.setOnClickListener(v -> {
            Intent intent = new Intent(mAppCompatActivity, CreateSign.class);
            bundle = mAppCompatActivity.getIntent().getExtras();
            bundle.putInt(ModelFieldConstants.COURSE_CHOOSE_SELECT, 0);
            intent.putExtras(bundle);
            intentActivityResultLauncher.launch(intent);
        });
        //创建班级
        btCreateClass.setOnClickListener(v -> {
            Intent intent = new Intent(mAppCompatActivity, CreateCourse.class);
            intent.putExtras(mAppCompatActivity.getIntent().getExtras());
            intentActivityResultLauncher.launch(intent);
        });
        //管理班级
        btManageClass.setOnClickListener(v -> vp2.setCurrentItem(1));
    }

}
